﻿Imports System.IO
Imports System.Web
Imports System.Text
Imports MeoTom.Core.DAL

Namespace BLL
    Public Class Comments
        Private util As New Utilities.Common()
        Private _CommentsID As Integer
        Private _AddedBy As String
        Private _AddedByEmail As String
        Private _AddedByIP As String
        Private _AddedDate As DateTime
        Private _Body As String
        Private _ArticleId As Integer
        Private _ArticlePath As String
        Private _ArticleTitle As String
        Private _Title As String
        Private _Keyword As String
        Private _AddedByURL As String
        Private _Status As Boolean
        Private _ArticlePathReturn As String
        Private _PageID As String
        Private _PagePath As String
        Private _PageTitle As String
        Private _IsAdmin As Boolean

        Public Property IsAdmin() As Boolean
            Get
                Return _IsAdmin
            End Get
            Set(ByVal value As Boolean)
                _IsAdmin = value
            End Set
        End Property

        Public Property PageTitle() As String
            Get
                Return _PageTitle
            End Get
            Set(ByVal value As String)
                _PageTitle = value
            End Set
        End Property
        Public Property PagePath() As String
            Get
                Return _PagePath
            End Get
            Set(ByVal value As String)
                _PagePath = value
            End Set
        End Property
        Public Property PageID() As String
            Get
                Return _PageID
            End Get
            Set(ByVal value As String)
                _PageID = value
            End Set
        End Property
        Public Property ArticlePathReturn() As String
            Get
                Return _ArticlePathReturn
            End Get
            Set(ByVal value As String)
                _ArticlePathReturn = value
            End Set
        End Property
        Public Property AddedByURL() As String
            Get
                Return _AddedByURL
            End Get
            Set(ByVal value As String)
                _AddedByURL = value
            End Set
        End Property
        Public Property Status() As Boolean
            Get
                Return _Status
            End Get
            Set(ByVal value As Boolean)
                _Status = value
            End Set
        End Property
        Public Property KeyWord() As String
            Get
                Return _Keyword
            End Get
            Set(ByVal value As String)
                _Keyword = value
            End Set
        End Property
        Public Property ArticleTitle() As String
            Get
                Return _ArticleTitle
            End Get
            Set(ByVal value As String)
                _ArticleTitle = value
            End Set
        End Property
        Public Property ArticlePath() As String
            Get
                Return _ArticlePath
            End Get
            Set(ByVal value As String)
                _ArticlePath = value
            End Set
        End Property
        Public Property Title() As String
            Get
                Return _Title
            End Get
            Set(ByVal value As String)
                _Title = value
            End Set
        End Property
        Public Property CommentsID() As Integer
            Get
                Return _CommentsID
            End Get
            Set(ByVal value As Integer)
                _CommentsID = value
            End Set
        End Property
        Public Property AddedBy() As String
            Get
                Return _AddedBy
            End Get
            Set(ByVal value As String)
                _AddedBy = value
            End Set
        End Property
        Public Property AddedByEmail() As String
            Get
                Return _AddedByEmail
            End Get
            Set(ByVal value As String)
                _AddedByEmail = value
            End Set
        End Property
        Public Property AddedByIP() As String
            Get
                Return _AddedByIP
            End Get
            Set(ByVal value As String)
                _AddedByIP = value
            End Set
        End Property
        Public Property AddedDate() As DateTime
            Get
                Return _AddedDate
            End Get
            Set(ByVal value As DateTime)
                _AddedDate = value
            End Set
        End Property
        Public Property Body() As String
            Get
                Return _Body
            End Get
            Set(ByVal value As String)
                _Body = value
            End Set
        End Property
        Public Property ArticleId() As Integer
            Get
                Return _ArticleId
            End Get
            Set(ByVal value As Integer)
                _ArticleId = value
            End Set
        End Property

        Public commentPrv As CommentProvider

        Public Sub New()
            commentPrv = New CommentProvider()
        End Sub

        Public Sub Create()
            commentPrv.Create(Me)
        End Sub

        Public Sub Update()
            commentPrv.Update(Me)
        End Sub

        Public Sub Delete()
            commentPrv.Delete(Me)
        End Sub

        Public Sub DeleteByArticle()
            commentPrv.DeleteByArticle(Me)
        End Sub

        Public Sub DeleteByPage()
            commentPrv.DeleteByPage(Me)
        End Sub

        Public Sub UpdateStatus()
            commentPrv.UpdateStatus(Me)
        End Sub

        Public Function BlockIP() As String
            Dim strReturn As String = ""
            Try
                Me.GetByID()

                Dim strOld As String = ""
                Dim strNew As String = ""
                Dim IPToBlock As String = Me.AddedByIP & "|"
                Dim reader As New StreamReader(Configs.FolderSaveData & "IPBlock.jt", System.Text.Encoding.UTF8)
                strOld = reader.ReadToEnd()
                reader.Close()

                strReturn = IPToBlock
                If Not strOld.Contains(IPToBlock) Then
                    strNew = strOld & IPToBlock

                    Dim writer As New StreamWriter(Configs.FolderSaveData & "IPBlock.jt", False, Encoding.UTF8)
                    writer.Write(strNew)
                    writer.Flush()
                    writer.Close()
                    strReturn &= "True"
                Else
                    strReturn &= "False"
                End If
            Catch ex As Exception

            End Try
            Return strReturn
        End Function

        Public Sub GetByID()
            Try
                Dim reader As SqlClient.SqlDataReader = commentPrv.GetByID(Me)
                If reader.Read() Then
                    Me.AddedBy = reader("AddedBy").ToString
                    Me.AddedByEmail = reader("AddedByEmail").ToString
                    Me.AddedByIP = reader("AddedByIP").ToString
                    Me.AddedDate = DateTime.Parse(reader("AddedDate").ToString)
                    Me.Body = reader("Body").ToString
                    Me.CommentsID = Integer.Parse(reader("CommentsID").ToString)
                    Me.AddedByURL = reader("AddedByURL").ToString()
                    Me.ArticleId = Integer.Parse(reader("ArticleId").ToString())
                End If
                reader.Close()
            Catch ex As Exception

            End Try
        End Sub

        Public Function GetByArticle() As DataSet
            Return commentPrv.GetByArticle(Me)
        End Function

        Public Function GetAll() As DataSet
            Return commentPrv.GetAll()
        End Function

        Public Function GetAllPageComment() As DataSet
            Return commentPrv.GetAllPageComment()
        End Function

        Public Function Find() As DataSet
            Return commentPrv.Find(Me)
        End Function

        ''' <summary>
        ''' Write article comment HTML
        ''' </summary>
        ''' <param name="TypeComment">0|1</param>
        ''' <remarks>If TypeComment = 0 Then Article Else Page</remarks>
        Public Function WriteAricleComment(ByVal TypeComment As Integer, Optional ByVal ArticleID As String = "") As Integer
            Dim strFileNameExt As String = ""
            Dim iPage As Integer = 0
            Dim TotalComment As Integer = 1
            Dim strOut As New StringBuilder()
            Dim strCmContent As New StringBuilder()
            Try
                If TypeComment = 0 Then
                    TotalComment = 1
                    Dim reader As SqlClient.SqlDataReader = commentPrv.GetByArticleLow(Me)
                    If reader.HasRows Then
                        While reader.Read
                            Me.ArticleId = Integer.Parse(reader("ArticlesId").ToString())
                            Me.AddedBy = reader("AddedBy").ToString()
                            Me.AddedDate = DateTime.Parse(reader("AddedDate").ToString()).AddHours(Configs.TimeLine)
                            Me.Body = util.BBCode(reader("Body").ToString())
                            Me.AddedByURL = reader("AddedByURL").ToString()
                            Me.ArticlePath = reader("ArticlePath").ToString()
                            Me.ArticleTitle = reader("Title").ToString()
                            strFileNameExt = reader("ArticlesId").ToString()

                            strCmContent.Append("<li id=""Comments_").Append(TotalComment).Append(""">")
                            If Me.AddedByURL = vbNullString Then
                                strCmContent.Append("<cite>").Append(Me.AddedBy).Append(":</cite><br />")
                            Else
                                strCmContent.Append("<cite><a href=""").Append(Me.AddedByURL).Append(""" target=""blank"">").Append(Me.AddedBy).Append(":</a></cite><br />")
                            End If
                            strCmContent.Append("<small class=""commentmetadata""><a href=""#Comments_").Append(TotalComment).Append(""">").Append(String.Format("{0:dd/MM/yyyy}", Me.AddedDate)).Append(" at ").Append(String.Format("{0:HH:mm tt}", Me.AddedDate)).Append("</a></small>")
                            strCmContent.Append("<p>").Append(Me.Body).Append("</p>")
                            strCmContent.Append("<span class=""number"">").Append(TotalComment).Append("</span>")
                            strCmContent.Append("</li>")

                            Me.ArticlePathReturn = Me.ArticlePath & "#Comments_" & TotalComment.ToString()

                            TotalComment = TotalComment + 1
                        End While
                    Else
                        strCmContent.Append("")
                        strFileNameExt = ArticleID
                    End If
                    reader.Close()
                ElseIf TypeComment = 1 Then
                    TotalComment = 1
                    Dim reader As SqlClient.SqlDataReader = commentPrv.GetByPageLow(Me)
                    If reader.HasRows Then
                        While reader.Read
                            Me.AddedBy = reader("AddedBy").ToString()
                            Me.AddedDate = DateTime.Parse(reader("AddedDate").ToString()).AddHours(Configs.TimeLine)
                            Me.Body = util.BBCode(reader("Body").ToString())
                            Me.AddedByURL = reader("AddedByURL").ToString()
                            Me.PagePath = reader("PagePath").ToString()
                            Me.PageTitle = reader("PageTitle").ToString()
                            strFileNameExt = reader("PageID").ToString()

                            strCmContent.Append("<li id=""Comments_").Append(TotalComment).Append(""">")
                            If Me.AddedByURL = vbNullString Then
                                strCmContent.Append("<cite>").Append(Me.AddedBy).Append(":</cite><br />")
                            Else
                                strCmContent.Append("<cite><a href=""").Append(Me.AddedByURL).Append(""" target=""blank"">").Append(Me.AddedBy).Append(":</a></cite><br />")
                            End If
                            strCmContent.Append("<small class=""commentmetadata""><a href=""#Comments_").Append(TotalComment).Append(""">").Append(String.Format("{0:dd/MM/yyyy}", Me.AddedDate)).Append(" at ").Append(String.Format("{0:HH:mm tt}", Me.AddedDate)).Append("</a></small>")
                            strCmContent.Append("<p>").Append(Me.Body).Append("</p>")
                            strCmContent.Append("<span class=""number"">").Append(TotalComment).Append("</span>")
                            strCmContent.Append("</li>")

                            Me.ArticlePathReturn = Me.PagePath & "#Comments_" & TotalComment.ToString()

                            TotalComment = TotalComment + 1
                        End While
                    Else
                        strCmContent.Append("")
                        strFileNameExt = ArticleID
                    End If
                    reader.Close()
                End If

                strOut.Append("<h3><a href=""#Comments"" name=""Comments"" id=""Comments"" title=""Bình luận"">Bình luận (").Append(TotalComment - 1).Append(")</a></h3><div class=""hr fl"">&nbsp;</div>")
                strOut.Append("<ol class=""CommentList"">")
                strOut.Append(strCmContent.ToString())
                strOut.Append("</ol>")

                Dim strFileName As String = Configs.CommentFile & "Comments_" & strFileNameExt & ".htm"
                If strCmContent.ToString().Length < 1 Then
                    util.DeleteFile(strFileName)
                Else
                    Dim writer As New StreamWriter(strFileName, False, System.Text.Encoding.UTF8)
                    writer.Write(strOut.ToString())
                    writer.Flush()
                    writer.Close()
                End If
            Catch ex As Exception

            End Try
            Return TotalComment
        End Function
    End Class
End Namespace
